﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.ModelConfiguration;
using System.Linq;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;

namespace KeyCore.Domain.Model
{
    public class DaneOsobowe
    {
       // [Key,DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public DaneOsobowe()
        {
            // do not create instances as they properties are marked required 
            //        Rodzice = new Rodzice();
            Adresy = new HashSet<Adres>();
            DokumentTozsamosci = new HashSet<DokumentTozsamosci>();
            KontoBankowe = new HashSet<KontoBankowe>();
            Telefon = new HashSet<Telefon>();
        }

        public System.Guid DaneOsoboweId { get; set; }
        public short PESEL { get; set; }
        public virtual Rodzice Rodzice { get; set; }
        public virtual ICollection<Adres> Adresy { get; set; }
        public virtual ICollection<DokumentTozsamosci> DokumentTozsamosci { get; set; }
        public virtual ICollection<KontoBankowe> KontoBankowe { get; set; }
        public virtual ICollection<Telefon> Telefon { get; set; }

        public bool HasValue
        {
            get
            {
                return (PESEL != 0) 
                    || (Rodzice != null) 
                    || (Adresy != null)
                    || (DokumentTozsamosci != null);
            }
        }
    }

    public class DaneOsoboweConfiguration : EntityTypeConfiguration<DaneOsobowe>
    {

        public DaneOsoboweConfiguration()
        {
            // bi-directional  one-to-one with Osoba so no need of key
        //    HasKey(k => k.DaneOsoboweId);
        //    Property(p => p.DaneOsoboweId)
        //        .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
            HasOptional(o => o.Rodzice)
                .WithRequired();
            HasMany(m => m.Adresy).WithRequired(r => r.DaneOsobowe);
            HasMany(m => m.DokumentTozsamosci).WithRequired(r => r.DaneOsobowe);
            HasMany(m => m.KontoBankowe).WithRequired(r => r.DaneOsobowe);
            HasMany(m => m.Telefon).WithRequired(r => r.DaneOsobowe);
        }
    }
}